﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HMS_Hospital_Team2._6.DO;

namespace HMS_Hospital_Team2._6.DA
{
    class BenhNhanDA
    {

        // Hàm load thông tin bệnh nhân
        public static List<BenhNhanDO> LoadThongTinBN_DA(string sMaBN)
        {
            List<BenhNhanDO> TTBenhNhan = new List<BenhNhanDO>();
            using (HMSHospitalEntities context = new HMSHospitalEntities())
            {
                var thongtinbn = from ttbn in context.BenhNhans
                                 where ttbn.MaBenhNhan == sMaBN
                                 select ttbn;
                foreach (var row in thongtinbn)
                {
                    BenhNhanDO thongtinBN = new BenhNhanDO();
                    thongtinBN.HoBenhNhan = row.HoBenhNhan;
                    thongtinBN.TenBenhNhan = row.TenBenhNhan;
                    thongtinBN.GioiTinh = (bool)row.GioiTinh;
                    thongtinBN.NamSinh = (int)row.NamSinh;
                    thongtinBN.DiaChi = row.DiaChi;
                    thongtinBN.NTNSinhBenhNhan = (DateTime)row.NTNSinhBenhNhan;

                    TTBenhNhan.Add(thongtinBN);
                }
                return TTBenhNhan;
            }
        }


        public static string LaytenBN_DA(string sMaBN)
        {
            string stenbenhnhan = "";
            using (HMSHospitalEntities context = new HMSHospitalEntities())
            {
                var tenbn = from tbn in context.BenhNhans
                                 where (tbn.MaBenhNhan == sMaBN)
                                 select tbn;
                BenhNhanDO us = new BenhNhanDO();
                foreach (var row in tenbn)
                {
                    us.TenBenhNhan = row.TenBenhNhan;
                }
                stenbenhnhan = us.TenBenhNhan.ToString();
                return stenbenhnhan;
            }
        }

        public static string LayhoBN_DA(string sMaBN)
        {
            string shobenhnhan = "";
            using (HMSHospitalEntities context = new HMSHospitalEntities())
            {
                var tenbn = from tbn in context.BenhNhans
                            where (tbn.MaBenhNhan == sMaBN)
                            select tbn;
                BenhNhanDO us = new BenhNhanDO();
                foreach (var row in tenbn)
                {
                    us.HoBenhNhan = row.HoBenhNhan;
                }
                shobenhnhan = us.HoBenhNhan.ToString();
                return shobenhnhan;
            }
        }
        // Hàm kiểm tra có tồn tại trong database
        public static bool Checkdatabase_DA(string sMaBenhNhan)
        {
            bool check = true;
            using (HMSHospitalEntities context = new HMSHospitalEntities())
            {
                var thongtinbn = from ttbn in context.BenhNhans
                                 select ttbn;
                foreach (var row in thongtinbn)
                {
                    if (row.MaBenhNhan == sMaBenhNhan)
                    {
                        check = true;
                        break;
                    }
                    else
                    {
                        check = false;
                    }
                }
                return check;
            }
        }
    }
}
